package com.qbkj.base_lib.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;


/**
 * View处理控制器
 * Created by St on 2018/4/12.
 */

public class ViewController {
    private View mView;
    private SparseArray<View> views;
    private Context mContext;
    private Window mWindow;

    public ViewController(Context context, Window window) {
        this.mContext = context;
        this.mWindow = window;
        views = new SparseArray<>();
    }

    public ViewController(Context context, View view) {
        this.mContext = context;
        this.mView = view;
        views = new SparseArray<>();
    }

    public Context getContext() {
        return mContext;
    }
    public void setGlideImageLoding(int viewId, String url) {
        ImageView textView = getView(viewId);
        Glide.with(mContext).load(url).into(textView);
    }

    public void setOnClickListener(View.OnClickListener listener, @IdRes int... viewIds) {
        for (int viewId : viewIds) {
            View view = getView(viewId);
            view.setOnClickListener(listener);
        }
    }

    public String getText(int viewId) {
        View view = views.get(viewId);
        if (view instanceof TextView) {
            return ((TextView) view).getText().toString();
        }
        return "";
    }


    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            if (mWindow != null) {
                view = mWindow.findViewById(viewId);
            } else if (mView != null) {
                view = mView.findViewById(viewId);
            } else {
                try {
                    throw new Exception("未初始化 父容器.");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            views.put(viewId, view);
        }
        return (T) view;
    }

    public void setClickable(int viewId, boolean clickable) {
        View view = getView(viewId);
        view.setClickable(clickable);
    }


    public TextView setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        return textView;
    }

    public TextView setText(int viewId, int res_id) {
        TextView textView = getView(viewId);
        textView.setText(res_id);
        return textView;
    }

    public TextView setTextColor(int viewId, int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(colorId);
        return textView;
    }

    public View setBackgroundColor(int viewId, int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
        return view;
    }

    public RatingBar setRating(int viewId, float rating, int max) {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return view;
    }

    public RatingBar setRating(int viewId, float rating) {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return view;
    }

    public ImageView setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return view;
    }

    public ImageView setImageDrawable(int viewId, int imgid) {
        ImageView view = getView(viewId);
        view.setImageResource(imgid);
        return view;
    }

    public ImageView setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return view;
    }


    public View setBackgroundResource(int viewId, int backgroundResource) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundResource);
        return view;
    }

    public View setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return view;
    }

    public View setVisibility(int viewId, int visible) {
        View view = getView(viewId);
        switch (visible) {
            case View.VISIBLE:
                view.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                view.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                view.setVisibility(View.GONE);
                break;
            default:
                break;
        }
        return view;
    }

    public ImageView setImageResource(int viewId, int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return view;
    }

    public ProgressBar setProgress(int viewId, int progress) {
        ProgressBar view = getView(viewId);
        view.setProgress(progress);
        return view;
    }

    public View setTag(int viewId, Object tag) {
        View view = getView(viewId);
        view.setTag(tag);
        return view;
    }

    public View setTag(int viewId, int key, Object tag) {
        View view = getView(viewId);
        view.setTag(key, tag);
        return view;
    }

    public View setChecked(int viewId, boolean checked) {
        View view = getView(viewId);
        if (view instanceof CompoundButton) {
            ((CompoundButton) view).setChecked(checked);
        } else if (view instanceof CheckedTextView) {
            ((CheckedTextView) view).setChecked(checked);
        }
        return view;
    }


    public View setRecyclerViewAdapter(int viewId, RecyclerView.Adapter adapter) {
        View view = getView(viewId);
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setAdapter(adapter);
        }
        return (RecyclerView) view;
    }

    public View setListViewAdapter(int viewId, BaseAdapter adapter) {
        View view = getView(viewId);
        if (view instanceof ListView) {
            ((ListView) view).setAdapter(adapter);
        }
        return (ListView) view;
    }

    public RecyclerView setRecyclerViewLayoutManager(int viewId, RecyclerView.LayoutManager layoutManager) {
        View view = getView(viewId);
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setLayoutManager(layoutManager);
        }
        return (RecyclerView) view;
    }

    public View scrollTo(int viewId, int x, int y) {
        View view = getView(viewId);
        view.scrollTo(x, y);
        return view;
    }


    public TextView getTextView(int viewId) {
        TextView view = (TextView) views.get(viewId);
        if (view == null) {
            view = getView(viewId);
            views.put(viewId, view);
        }
        return view;
    }

    public EditText getEditText(int viewId) {
        EditText view = (EditText) views.get(viewId);
        if (view == null) {
            view = getView(viewId);
            views.put(viewId, view);
        }
        return view;
    }

    public ImageView getImageView(int viewId) {
        ImageView view = (ImageView) views.get(viewId);
        if (view == null) {
            view = getView(viewId);
            views.put(viewId, view);
        }
        return view;
    }


}
